<template>
  <div class="login-setting">
    <el-form
      ref="userForm"
      :model="userInfo"
      :rules="relus"
      label-width="120px"
      style="margin-left: 120px; margin-top: 30px"
    >
      <el-form-item label="姓名:" prop="username">
        <el-input v-model="userInfo.username" style="width:300px" />
      </el-form-item>
      <el-form-item label="密码:" prop="password">
        <el-input v-model="userInfo.password" type="password" style="width:300px" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="doudi">更新</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getUserDetailById } from '@/api/user'
import { saveUserDetailById } from '@/api/employees'
export default {
  data() {
    return {
      userInfo: {
        username: '',
        password: ''
      },
      relus: {
        username: [
          { required: true, message: '用户名不能为空', triggler: 'blur' }
        ],
        password: [{ required: true, message: '密码不能为空', triggler: 'blur' }]
      }
    }
  },
  created() {
    this.loadUserDetailById()// 发送数据请求
  },
  methods: { // 发送数据请求
    async loadUserDetailById() {
      const res = await getUserDetailById(this.$route.query.id)
      console.log(res)
      this.userInfo = res.data
    },
    async doSave() { // 发送资料保存请求
      const res = await saveUserDetailById(this.userInfo)
      console.log(res)
    },
    doudi() { // 兜底检验
      this.$refs.userForm.validate(valid => {
        if (valid) {
          this.doSave()// 通过检验再发请求
          this.$message.success('编辑成功')
          this.$router.back()// 返回修改页
        }
      })
    }

  }
}
</script>
